# 写一个pytest用例，测试电商主流程（11个步骤），定义一个类，在类中先写好前两个用例，第三步写一个函数头。
# 电商主流程( P0级别基线用例 ）：
# ①　登录
# ②　获取一级分类
# ③　搜索指定商品，关键词:劳力士
# ④　获取搜索结果第一个商品信息
# ⑤　放入购物车
# ⑥　购物车列表
# ⑦　添加收货地址
# ⑧　购买步骤1
# ⑨　购买步骤2，生成订单
# ⑩　获取订单列表
# ⑪　注销
import common.netTools
from config.config import host
import pytest

@pytest.fixture(scope="class" ,autouse=True)
def init_and_clear():
    print("setup")
    #yield之前的代码执行初始化工作，yield之后的代码执行清理工作
    yield
    print("teardown")
class TestLogin():

    def test_01_longin_success(self):
        #1.准备url和数据
        url = host+"/mobile/index.php?act=login"
        d={
            "username":"caohongbo",
            "password":"123456789",
            "client":"android"
        }
        j = common.netTools.post(url,d,is_success=True)
        #8.提取token，放到全区变量中待用
        token = j["datas"]["key"]
        globals()["token"] = token

    def test_02_getclass_first_success(self):
        #1.准备url和数据
        url = host+"/mobile/index.php?act=goods_class"

        j=common.netTools.get(url)
        #8.后置处理
        class_list = j["datas"]["class_list"]
        assert len(class_list)>0
        for c in class_list:
            gc_id = c["gc_id"]
            gc_name= c["gc_name"]
            print("gc_id:%s,gc_name:%s"%(gc_id,gc_name))

    def test_03_search(self):
        url=host+"/mobile/index.php?act=goods&op=goods_list&page=100"
        p={'keyword':"劳力士"}
        j=common.netTools.get(url,params=p)
        goods_list=j["datas"]["goods_list"]
        assert len(goods_list)>0
        good=goods_list[0]
        goods_id = good.get("goods_id")
        assert goods_id is not None
        globals()["goods_id"]=goods_id

    def test_04_search(self):
        url=host+"/mobile/index.php?act=goods&op=goods_detail"
        p={"goods_id":"100009"}
        j=common.netTools.get(url,params=p)
        goods_info=j["datas"]["goods_info"]
        assert goods_info is not None
        goods_id = goods_info["goods_id"]
        assert goods_id is not None
    def test_05_search(self):
        url=host+"/mobile/index.php?act=member_cart&op=cart_add"
        d={"goods_id":globals()["goods_id"],"key":globals()["token"],"quantity":1}
        j=common.netTools.post(url,data=d)
        code=j["code"]
        datas=j["datas"]
        assert  code ==200
        assert datas == "1"

    def test_06_search(self):
        url=host+"/mobile/index.php?act=member_cart&op=cart_list"
        d={"key":globals()["token"]}
        j=common.netTools.post(url,data=d)
        cart_list = j["datas"]["cart_list"]
        assert len(cart_list)>0
        cartid_list = []
        isfind = False
        # 遍历购物车
        for c in cart_list:
            goods = c["goods"]
            # 遍历某商铺的所有商品
            for g in goods:
                goods_id_in_cart = g["goods_id"]
                if goods_id_in_cart==globals()["goods_id"]:
                    cartid_list.append(g["cart_id"]+"|"+str(1))
                    isfind=True
                    break;
            if isfind:
                break
        assert isfind  == True
        cartid_all = ",".join(cartid_list)
        globals()["cartid_all"] = cartid_all

    def test_07_search(self):
        url=host+"/mobile/index.php?act=member_address&op=address_add"
        d={"key":globals()["token"],"true_name":"曹宏博","mob_phone":17733203442,"city_id":36,"area_id":49,
           "address":"北京市昌平区回龙观西大街3号","area_info":"北京市昌平区回龙观西大街3号"}
        j=common.netTools.post(url,data=d)
        code=j["code"]
        assert code == 200
        address_id=j["datas"]["address_id"]
        assert address_id is not None
        globals()["address_id"]=address_id

    def test_08_search(self):
        url=host+"/mobile/index.php?act=member_buy&op=buy_step1"
        d={"key":globals()["token"],"cart_id":globals()["cartid_all"],"ifcart":1}
        j=common.netTools.post(url,data=d)
        store_cart_list = j["datas"]["store_cart_list"]
        assert store_cart_list is not None
        isfind = False
        for v in store_cart_list.values():
            goods_list = v["goods_list"]
            for g in goods_list:
                goods_id_in_cart = g["goods_id"]
                if goods_id_in_cart == globals()["goods_id"]:
                    isfind=True
                    break
            if isfind:
                break
        assert isfind==True

    def test_09_search(self):
        url=host+"/mobile/index.php?act=member_buy&op=buy_step2"
        d={"key":globals()["token"],
           "ifcart":1,
           "cart_id":globals()["cartid_all"],
           "address_id":globals()["address_id"],
           "vat_hash": "lRsmWUPoq7kKdOlObvFbMuMhfxSiCrtycKH",
            "offpay_hash": "UYIwQZYT3U754H-Fn2WP8lBlOg-Vc0ydsjRPNm2",
            "offpay_hash_batch": "OneKenbINRXUwiOVDNclcQ3mH7IchQU",
            "pay_name": "online",
            "invoice_id": "0",
            "voucher": "",
            "pd_pay": "",
            "password": "",
            "fcode": "",
            "rcb_pay": "0",
            "rpt": "",
            "pay_message": "1"}
        j=common.netTools.post(url,data=d)
        pay_sn=j["datas"]["pay_sn"]
        assert pay_sn is not None
        globals()["pay_sn"]=pay_sn

    def test_10_search(self):
        url=host+"/mobile/index.php?act=member_order&op=order_list&page=10000&curpage=1"
        d={"key":globals()["token"]}
        j=common.netTools.post(url,data=d)
        order=j["datas"]["order_group_list"][0]["order_list"][0]
        assert order is not None
        pay_sn_in_order=order["pay_sn"]
        assert pay_sn_in_order == globals()["pay_sn"]

    def test_11_search(self):
        url=host+"/mobile/index.php?act=logout"
        d={"key":globals()["token"],"client":"android","username":"caohongbo"}
        j=common.netTools.post(url,data=d)

